package club.hicode.daydayup.oshi;

import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONUtil;
import org.junit.Test;
import oshi.SystemInfo;
import oshi.hardware.*;
import oshi.software.os.FileSystem;
import oshi.software.os.OSFileStore;
import oshi.software.os.OSProcess;
import oshi.software.os.OperatingSystem;
import oshi.software.os.mac.MacFileSystem;

/**
 * 进程信息
 * @author Liu Chunfu
 * @date 2018-08-24 上午11:41
 **/
public class PidTest {

	@Test
	public void test1() {
		SystemInfo si = new SystemInfo();
		//硬件信息
		HardwareAbstractionLayer hal = si.getHardware();


		//软件信息
		OperatingSystem os = si.getOperatingSystem();
	}

	@Test
	public void testHardware() {
		SystemInfo si = new SystemInfo();
		//硬件信息
		HardwareAbstractionLayer hal = si.getHardware();
		CentralProcessor processor = hal.getProcessor();
		double systemCpuLoad = processor.getSystemCpuLoad();

		//内存信息
		GlobalMemory memory = hal.getMemory();
		long available = memory.getAvailable();
		long total = memory.getTotal();
		//虚拟内存
		long swapUsed = memory.getSwapUsed();

		System.out.println(swapUsed);
	}


	@Test
	public void testOS() {
		SystemInfo info = new SystemInfo();

		OperatingSystem os = info.getOperatingSystem();

		FileSystem fileSystem = os.getFileSystem();
		OSFileStore[] fileStores = fileSystem.getFileStores();

		for (OSFileStore fileStore : fileStores) {
			long totalSpace = fileStore.getTotalSpace();
			long usableSpace = fileStore.getUsableSpace();
			double v = Convert.toDouble(usableSpace) / Convert.toDouble(totalSpace);
			String name = fileStore.getName();
			System.out.println(name + ":" + (1 - v));
		}

		//可以获取CPU占用
		//OSProcess[] processes = os.getProcesses(5, OperatingSystem.ProcessSort.CPU);
		//for (OSProcess process : processes) {
		//	System.out.println(JSONUtil.toJsonStr(process));
		//	System.out.println("=======");
		//}
		//
		//int threadCount = os.getThreadCount();
		//System.out.println(threadCount);

	}

	@Test
	public void testOs() {
		SystemInfo info = new SystemInfo();

		FileSystem fileSystem = info.getOperatingSystem().getFileSystem();

		HardwareAbstractionLayer hardware = info.getHardware();
		//HWDiskStore[] diskStores = hardware.getDiskStores();
		//
		//for (HWDiskStore  diskStore : diskStores) {
		//	System.out.println(diskStore);
		//
		//
		//}

		HardwareAbstractionLayer hardware1 = info.getHardware();


		//FileSystem fileSystem = new MacFileSystem();
		OSFileStore[] fileStores = fileSystem.getFileStores();
		for (OSFileStore fileStore : fileStores) {
			long totalSpace = fileStore.getTotalSpace();

		}

	}

	@Test
	public void osStore() {

	}
}
